<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\Ad;
use app\models\AdPosition;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class AdController extends BaseController
{
    public $type_arr = ['' ,'轮播图' ,'视频', '图片'];
    /**
     * @param string $type
     * @return array
     */
    public function actionAdPositionList(){
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', '');
        $ad_position_query = AdPosition::find()
            ->where(['status' => [0,1]]);
        if ($name) {
            $ad_position_query->andWhere(['like', 'name', $name]);
        }
        $ad_position_list = $ad_position_query->orderBy("created_at desc")->limit(AdPosition::PAGE_SIZE)->offset(AdPosition::PAGE_SIZE*$page)->all();
        $ad_position_data = [];
        if ($ad_position_list) {
            foreach ($ad_position_list as $v) {
                $ad_position_data[] = [
                    'id' => $v->id,
                    'name' => $v->name,
                    'code' => $v->code,
                    'status' => $v->status,
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $ad_position_data,
            'totalPage' => $ad_position_query->count(),
            'currentPage' => $page,
            'perPage' => AdPosition::PAGE_SIZE
        ]);
    }

    public function actionEditAdPosition(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        return $this->edit(AdPosition::class, $id, $post);
    }

    public function actionChangeAdPositionStatus(){
        $id = Yii::$app->request->post('id', 0);
        $status = Yii::$app->request->post('status', 0);
        $adPosition = AdPosition::findOne($id);
        if (empty($adPosition)) {
            return Re::data(null, '广告位信息读取失败', Re::ERROR);
        }
        $adPosition->status = $adPosition->status ? 0 : 1;
        if (!$adPosition->save()) {
            return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }

    public function actionAdList(){
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', '');
        $ad_query = Ad::find()->where(['status' => [0,1]]);
        if ($name) {
            $ad_query->andWhere(['like', 'name', $name]);
        }
        $ad_list = $ad_query->orderBy("created_at desc")->limit(Ad::PAGE_SIZE)->offset(Ad::PAGE_SIZE*$page)->all();
        $ad_data = [];
        if ($ad_list) {
            foreach ($ad_list as $v) {
                $ad_data[] = [
                    'id' => $v->id,
                    'pid' => $v->pid,
                    'cover' => $v->cover,
                    'target' => $v->target,
                    'type' => $v->type,
                    'video_url' => $v->video_url,
                    'type_name' => $this->type_arr[$v->type],
                    //'position_name' => $v->position->name,
                    'status' => $v->status,
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $ad_data,
            'totalPage' => $ad_query->count(),
            'currentPage' => $page,
            'perPage' => Ad::PAGE_SIZE
        ]);
    }

    public function actionEditAd(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        return $this->edit(Ad::class, $id, $post);
    }

    public function actionChangeAdStatus(){
        $id = Yii::$app->request->post('id', 0);
        $status = Yii::$app->request->post('status', 0);
        $ad = Ad::findOne($id);
        if (empty($ad)) {
            return Re::data(null, '广告信息读取失败', Re::ERROR);
        }
        $ad->status = $status;
        if (!$ad->save()) {
            return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }

    public function actionAdPositionListForSelect(){
        $ad_position_list = AdPosition::find()->where(['status' => 1])->select('id, name')->all();
        return Re::data(['data' => $ad_position_list]);
    }
}
